﻿using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.Mvc;
using DevFramework.Business.Abstract;
using DevFramework.Entities.Concrete;
using DevFramework.Business.Abstract;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace DevFramework.MvcUI.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        private readonly IProductService _productService;
        private readonly ICategoryService _categoryService;

        public ProductController(IProductService productService, ICategoryService categoryService)
        {
            _productService = productService;
            _categoryService = categoryService;
        }

        public ActionResult DemoAdmin()
        {
            ViewData["categories"] = _categoryService.GetAll();
            return View(_productService.GetAll());
        }

        public ActionResult Index()
        {
            ViewData["categories"] = _categoryService.GetAll();
            return View(_productService.GetAll());
        }

        public void PictureSave(HttpPostedFileBase picture)
        {
            var ms = new MemoryStream();
            picture.InputStream.CopyTo(ms);

            TempData["picture"] = ms.ToArray();
        }

        public void PictureRemove()
        {
            TempData["picture"] = null;
        }

        public void FileSave(HttpPostedFileBase file)
        {
            var ms = new MemoryStream();
            file.InputStream.CopyTo(ms);

            TempData["file"] = ms.ToArray();
        }

        public void FileRemove()
        {
            TempData["file"] = null;
        }

        public JsonResult ReadUrunler(DataSourceRequest request)
        {
            return Json(_productService.GetAll().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Create([DataSourceRequest] DataSourceRequest request, Product product)
        {
            if (product != null && ModelState.IsValid)
            {
                product.Picture = (byte[])TempData["picture"];
                product.FileContent = (byte[])TempData["file"];
                _productService.Add(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Update([DataSourceRequest] DataSourceRequest request, Product product)
        {
            if (product != null && ModelState.IsValid)
            {
                _productService.Update(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, Product product)
        {
            if (product != null)
            {
                _productService.Delete(product);
            }

            return Json(new[] { product }.ToDataSourceResult(request, ModelState));
        }

        public ActionResult Download(int productId)
        {
            var urun = _productService.Get(productId);

            return File(urun.FileContent, "text/plain", "file.txt");
        }

    }
}